<!DOCTYPE html>
<!--
 *  创建时间: 2020-08-05-15:45
 *  作   者: lpc
 *  Email : lpc@hll520.cn
-->
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">
    <title>商品列表</title>
    <!-- JQuery -->
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <!-- Bootstrap核心CSS和JS -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script>
        $(function () {

            getData();

            /* 获取用户信息 */
            $.getJSON(
                "/user/getInfo",
                {},
                function (json) {
                    if (json.statusCode !== 200) {

                    } else {
                        $("#nav_login").html(`
                        <a href="/space">
                        <span class="glyphicon glyphicon-user"></span>
                        <span id="userName">${json.data.realname}</span></a>
                        `);

                        $("#nav_register").html(`
                    <a href="/user/signOut">安全退出</a>
                    `);

                    }
                },
                "json"
            );
        })

        var pageNow = 0;
        var pageAll = 1;
        var pageSize = 6;


        /* 获取数据 */
        function getData() {
            if (pageNow >= pageAll) {
                $("#nextMore").html("没有更多了哦！");
                return false;
            }
            pageNow += 1;
            $.getJSON(
                location.href + "/getData",
                {pageIndex: pageNow, pageSize: pageSize},
                function (json) {
                    if (json.statusCode !== 200) {
                        $("#searchVar").html("没有找到相关内容 " + json.msg);
                        return false;
                    }

                    // $("#sumRows").html(json.data.total);
                    pageNow = json.data.pageNum;
                    $("#pageNum").html(pageNow);
                    pageAll = json.data.pages;
                    if (json.data.size == 0 || json.data.list.length < 1) {
                        $("#nextMore").html("没有更多了哦！");
                        $("#searchVar").html("没有找到相关内容 " + json.msg);
                        return false;
                    }

                    for (let i = 0; i < json.data.list.length; i++) {
                        let goods = json.data.list[i];
                        if (goods.image == null || goods.image === "")
                            goods.image = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596716481337&di=fab03d71304d4827ecde2e1031b53a8e&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Ffc1f4134970a304e9b7f6625d4c8a786c8175c94.jpg";
                        $("#goodsVal").append(`
        <div class="col-md-4 column">
            <div class="row clearfix img-rounded" style="background: #eeeeee;padding-top: 10px;margin: 10px">
                <div class="col-md-12 column">
                    <img alt="商品预览图" src="${goods.image}" style="width: 100%" class="img-rounded"/>
                    <div class="panel panel-default" style="margin-top: 20px;width: 100%">
                        <div class="panel-heading">
                            <a href="/goods/info/${goods.id}">
                                <h3 class="panel-title">${goods.name}</h3>
                            </a>
                        </div>
                        <div class="panel-body text-right">
                            <span style="font-size: 18px">￥</span><span
                                style="color: red;font-size: 26px">${goods.price/100}</span>
                        </div>
                        <div class="panel-footer">
                            <a href="javaScript:addBuyCars(${goods.id})">
                            <button type="button" class="btn btn-default btn-block">加入购物车</button>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
                      `);

                    }
                },
                "json"
            );
        }

        /* 加入购物车 */
        function addBuyCars(GoodsID) {
            $.getJSON(
                "/shopping/addShop",
                {goodsId: GoodsID},
                function (json) {
                    if (json.statusCode === 302) {
                        alert("添加失败！" + json.msg);
                        location.href = json.data;
                        return false;
                    } else if (json.statusCode !== 200) {
                        alert("添加失败！" + json.msg);
                        return false;
                    }
                    alert("添加成功");
                }
            )
        }

        /* 搜索 */
        function search() {
            let key = $("#key").val();
            location.href = "/goods/search/" + key;
        }

    </script>
</head>
<body>
<div class="container">

    <!-- 导航 -->
    <div class="row clearfix">
        <div class="col-md-12 column">
            <!-- 导航 -->
            <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse"
                            data-target="#bs-example-navbar-collapse-1"><span
                            class="sr-only">WTUShop导航栏</span><span class="icon-bar"></span><span
                            class="icon-bar"></span><span class="icon-bar"></span></button>
                    <a class="navbar-brand" href="/">WTUShop</a>
                </div>

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <!-- 左边 -->
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="/goods/search">商场</a></li>
                        <li><a href="/kill/list">秒杀活动</a></li>
                        <li><a href="/space/">个人中心</a></li>
                        <li><a href="/admin/manage">控制台</a></li>
                    </ul>
                    <!-- 右边 -->
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="/shopping/list">购物车</a></li>
                        <li><a href="/order/list">订单查询</a></li>
                        <li id="nav_login"><a href="/login"><span
                                class="glyphicon glyphicon-user"></span><span id="userName">请登录</span></a></li>
                        <li id="nav_register"><a href="/register">注册</a></li>
                        <li>&nbsp;&nbsp;</li><!-- 占位符 -->
                    </ul>
                </div>

            </nav>
        </div>
    </div>

    <!-- 标题和搜索 -->
    <div class="row clearfix page-header">
        <div class="col-md-8 column" style="margin-top: 40px">
            <a href="/"><h1>WTUShop</h1></a>
        </div>

        <div class="input-group col-md-4 column " style="margin-top: 60px">
            <input type="text" class="form-control" placeholder="输入关键字" id="key">
            <span class="input-group-btn">
                <a href="javaScript:search()">
                <button id="btn_search" class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 搜索
                </button>
                    </a>
                    &nbsp;
            </span>
        </div>
    </div>

    <!-- 导航路径 -->
    <ul class="breadcrumb">
        <li><a href="/home">商场</a></li>
        <li><a href="/goods/search">商品列表</a></li>
        <li class="active" id="nowSearch">全部商品</li>
    </ul>

    <!-- 内容框 -->
    <div class="row clearfix" id="goodsVal">
        <!-- 动态加载 -->

    </div>

    <!-- 加载更多 -->
    <div class="row clearfix">
        <ul class="pager" id="nextMore">
            <li><a href="javaScript:getData()">加载更多</a></li>
        </ul>
    </div>

</div><!-- 自适应结尾（第一个div） -->

<!-- 尾部-->
<div style="width: 100%;margin-top: 60px;padding:35px;background: silver;">
    <p class="text-center">
        WTUShop是一个简易的线上购物商场，最初由 <strong>@Lengpucheng</strong>
        编写，作为SSM的练习作业.
    </p>
    <p class="text-center">
        Power by <a href="mailto:lpc@hll520.cn">@冷朴承</a> ,
        Personal Website for <a href="http://www.hll520.cn">hll520.cn</a>
    </p>

</div>

</body>
</html>